import {
    Subscribe
} from './subscribe.model'
const subscribe = new Subscribe()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        station: {},
        active: 0,
        date: [],
        subscribe: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        options.goods_price = parseFloat(options.goods_price)
        that.setData({
            station: options
        })
        that._loadData()
    },

    /**
     * 获取所有数据
     */
    _loadData() {
        let that = this
        that.getDate()
        that.getFirstSubscribeData()
    },

    /**
     * 获取7天内日期
     */
    getDate() {
        let that = this
        subscribe.getDate().then((res) => {
            that.setData({
                date: res.data
            })
        })
    },

    /**
     * 获取当天预约
     */
    getFirstSubscribeData() {
        let that = this,
            param = {
                company_id: that.data.station.id
            }
        subscribe.getSubscribeData(param).then((res) => {
            that.setData({
                subscribe: res.data
            })
        })
    },

    /**
     * 根据日期获取预约情况
     * @param {*} param
     */
    getSubscribeData(param) {
        let that = this
        subscribe.getSubscribeData(param).then((res) => {
            that.setData({
                subscribe: res.data
            })
        })
    },

    /**
     * 切换日期
     * @param {*} options
     */
    onSwitchStatus(options) {
        let that = this,
            id = subscribe.getDataSet(options, 'id'),
            week = subscribe.getDataSet(options, 'week'),
            status = subscribe.getDataSet(options, 'status'),
            active = subscribe.getDataSet(options, 'index'),
            param = {
                date_id: id,
                company_id: that.data.station.id
            }
        that.setData({
            active: active,
        })
        if ((week == 0 && status != 2) || (status == 1)) {
            that.setData({
                subscribe: []
            })
        } else {
            that.getSubscribeData(param)
        }
    },

    /**
     * 申请检车
     * @param {*} options
     */
    onApply(options) {
        let that = this,
            subscribe_id = subscribe.getDataSet(options, 'id'),
            subscribe_date = subscribe.getDataSet(options, 'subscribe_date'),
            station = that.data.station,
            param = `id=${station.id}&vehicle_name=${station.vehicle_name}&goods_id=${station.goods_id}&goods_name=${station.goods_name}&goods_price=${station.goods_price}&vehicle_id=${station.vehicle_id}&station_name=${station.station_name}&station_address=${station.station_address}&subscribe_id=${subscribe_id}&subscribe_date=${subscribe_date}`
        wx.navigateTo({
            url: `/pages/apply-sub/apply-sub?${param}`
        })
    }
})